<?php
use Tymon\JWTAuth\Exceptions\TokenExpiredException;

/*
|--------------------------------------------------------------------------
| Register The Laravel Class Loader
|--------------------------------------------------------------------------
|
| In addition to using Composer, you may use the Laravel class loader to
| load your controllers and models. This is useful for keeping all of
| your classes in the "global" namespace without Composer updating.
|
*/

ClassLoader::addDirectories(array(

	app_path().'/commands',
	app_path().'/controllers',
	app_path().'/models',
	app_path().'/database/seeds',
	app_path().'/pay/lib',
	app_path().'/pay/unit'		

));

/*
|--------------------------------------------------------------------------
| Application Error Logger
|--------------------------------------------------------------------------
|
| Here we will configure the error logger setup for the application which
| is built on top of the wonderful Monolog library. By default we will
| build a basic log file setup which creates a single file for logs.
|
*/

//Log::useFiles(storage_path().'/logs/laravel.log');
$logFile = 'webpc.log';
Log::useDailyFiles(storage_path().'/logs/'.$logFile);
/*
|--------------------------------------------------------------------------
| Application Error Handler
|--------------------------------------------------------------------------
|
| Here you may handle any errors that occur in your application, including
| logging them or displaying custom views for specific errors. You may
| even register several error handlers to handle different types of
| exceptions. If nothing is returned, the default error view is
| shown, which includes a detailed stack trace during debug.
|
*/

App::error(function(Exception $exception, $code)
{
	if(!Config::get('app.debug')) {
		header("Access-Control-Allow-Origin: *");
		header("Access-Control-Allow-Headers: Authorization,X-Requested-With,Accept,Content-Type");
		Log::error(sprintf('Request url: %s', Request::url()));
		Log::error(sprintf('Request payload: %s', json_encode(Request::all())));
		Log::error($exception);
		if (get_class($exception) == 'Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException') {
			return Response::view('errors.405_json', ['data' => sprintf('HTTP方法%s是不支持的', Request::getMethod())])->header('Content-Type', 'application/json')->setStatusCode(405);
		} else {
			return Response::view('errors.503_json', ['data' => '服务器暂时不能响应您的请求，请稍候再试。'])->header('Content-Type', 'application/json')->setStatusCode(503);
		}

		if (!Config::get('app.debug')) {
			return Response::view('errors.server', [], $code);
		}
	}
});

App::error(function(TokenExpiredException $exception, $code)
{
	return Response::view(
        'errors.400_json',
        array('data' => $exception->getMessage())
    )->header(
    	'Content-Type', 'application/json'
    )->setStatusCode($exception->getStatusCode());
});

App::missing(function($exception)
{
	header("Access-Control-Allow-Origin: *");
	header("Access-Control-Allow-Headers: Authorization,X-Requested-With,Accept,Content-Type");
	return Response::view('errors.404', array(), 404);
});

/*
|--------------------------------------------------------------------------
| Maintenance Mode Handler
|--------------------------------------------------------------------------
|
| The "down" Artisan command gives you the ability to put an application
| into maintenance mode. Here, you will define what is displayed back
| to the user if maintenance mode is in effect for the application.
|
*/

App::down(function()
{
	return Response::make("Be right back!", 503);
});

/*
|--------------------------------------------------------------------------
| Require The Filters File
|--------------------------------------------------------------------------
|
| Next we will load the filters file for the application. This gives us
| a nice separate location to store our route and application filter
| definitions instead of putting them all in the main routes file.
|
*/

/*
|--------------------------------------------------------------------------
| 自定义启动文件
|--------------------------------------------------------------------------
|
*/
require_once $app['path'].'/start/listen.php';

$password = Hash::make('123456');
// echo $password;die;
require app_path().'/filters.php';
